﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Controls;

namespace WpfDecorator
{
    public class MaskAdorner : Adorner
    {
        Rectangle rec;
        VisualCollection visualChildren;
        Button btnClose;

        public MaskAdorner(UIElement adornerElement)
            : base(adornerElement)
        {
            visualChildren = new VisualCollection(this);
            AddRectangle(adornerElement);
            btnClose = new Button() { Content = "CLose", Width = 100, Height = 25 };
            visualChildren.Add(btnClose);
            btnClose.Click += new RoutedEventHandler(btnClose_Click);
            Loaded += new RoutedEventHandler(MaskAdorner_Loaded);
        }

        void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Hidden();
        }

        void MaskAdorner_Loaded(object sender, RoutedEventArgs e)
        {
            Show();
        }

        public void Show()
        {
            rec.BeginAnimation(Rectangle.OpacityProperty, new DoubleAnimation() { To = 1, Duration = TimeSpan.FromSeconds(10) });
            rec.BeginAnimation(Rectangle.WidthProperty, new DoubleAnimation() { To = this.DesiredSize.Width });
            rec.BeginAnimation(Rectangle.HeightProperty, new DoubleAnimation() { To = this.DesiredSize.Height });
        }

        public void Hidden()
        {
            rec.BeginAnimation(Rectangle.OpacityProperty, new DoubleAnimation() { To = 0 });
            rec.BeginAnimation(Rectangle.WidthProperty, new DoubleAnimation() { To = 0 });
            rec.BeginAnimation(Rectangle.HeightProperty, new DoubleAnimation() { To = 0 });
        }

        void AddRectangle(UIElement adornerElement)
        {
            rec = new Rectangle();
            rec.Fill = new SolidColorBrush(Color.FromArgb((byte)200, Colors.LightBlue.R, Colors.LightBlue.G, Colors.LightBlue.B));
            visualChildren.Add(rec);
        }

        protected override System.Windows.Media.Visual GetVisualChild(int index)
        {
            return rec;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            rec.Width = this.DesiredSize.Width;
            rec.Height = this.DesiredSize.Height;

            rec.Arrange(new Rect(0, 0, this.DesiredSize.Width, this.DesiredSize.Height));
            btnClose.Arrange(new Rect(50, 50, 100, 25));
            return base.ArrangeOverride(finalSize);
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }
    }
}
